package com.glela.controller;

import org.apache.ibatis.exceptions.TooManyResultsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.constant.BaseConstants;
import com.glela.goods.model.request.GoodsRequest;
import com.glela.goods.service.ProductService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.User;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;

@Controller
@RequestMapping("product")
public class ProductController {

	private static Logger logger = LoggerFactory.getLogger(ProductController.class);

	@Autowired
	private ProductService productService;
	@Autowired
	private UserService userService;

	/**
	 * 查询货品
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getProductByGoodIdAndSpecInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getProductByGoodIdAndSpecInfo(@RequestBody GoodsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getGoodId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getSpecInfo())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int shareUserRoleId = 0;
			int userRoleId = 0;
			if (request.getShareUserId() > 0) {
				User shareUser = userService.getUser(request.getShareUserId());
				if (shareUser != null) {
					shareUserRoleId = shareUser.getRoleId();
				}
			}
			if (request.getUserId() != null && request.getUserId() > 0) {
				User user = userService.getUser(request.getUserId());
				if (user != null) {
					userRoleId = user.getRoleId();
				}
			}
			return productService.getProductByGoodIdAndSpecInfo(request.getGoodId(), request.getSpecInfo(),
					shareUserRoleId, userRoleId);
		} catch (Exception e) {
			logger.error("查询货品异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 查询货品
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getProductInfoByGoodIdAndSpecInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getProductInfoByGoodIdAndSpecInfo(@RequestBody GoodsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getGoodId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getSpecInfo())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			//店主用户ID  默认校妆姐姐店铺的店主ID
			Integer storeUserId = Integer.valueOf(BaseConstants.XZ_SISTER+"");
			if (!ParameterUtils.validParams(request.getUserId())) {
				User user = this.userService.getUserById(request.getUserId());
				User storeUser =this.userService.getUserByPersionId(user.getRecommendFrom());
				storeUserId = storeUser.getId().intValue();
			}
			return productService.getProductInfoByGoodIdAndSpecInfo(request.getGoodId(), request.getSpecInfo(), storeUserId);
		} catch(TooManyResultsException te) {
			logger.error("查询货品异常", "goodsid:" + request.getGoodId() + ": 出现在两个分时抢购时间段! ");
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}catch (Exception e) {
			logger.error("查询货品异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
}
